

September 10 + 12, 2024
pivotingimage credit: https://www.garrickadenbuie.com/project/tidyexplain/
pivot_longer() makes the data frame “longer” – many columns into a few columns (more rows):pivot_longer(data,cols,names_to=,value_to=)
pivot_wider() makes the data frame “wider” – a few columns into many columns (fewer rows):pivot_wider(data,names_from=,values_from=)


pivot_longerpivot_longer will be demonstrated using datasets from GapMinder.
litF represents country, year, and female literacy rate.
pivot_longerpivot_longerGDP represents country, year, and gdp (in fixed 2000 US$).
pivot_widerpivot_wider will be demonstrated using the babynames dataset.
pivot_wider babynamespivot_wider babynamespivot_wider babynames_joiningSee the Posit cheatsheets on wrangling & joining and pivoting.
left_join returns all rows from the left table, and any rows with matching keys from the right table.inner_join returns only the rows in which the left table have matching keys in the right table (i.e., matching rows in both sets).full_join returns all rows from both tables, join records from the left which have matching keys in the right table.Good practice: always specify the by argument when joining data frames.
10 women in science who changed the world (source: Discover Magazine)1
| name | profession |
|---|---|
| Ada Lovelace | Mathematician |
| Marie Curie | Physicist and Chemist |
| Janaki Ammal | Botanist |
| Chien-Shiung Wu | Physicist |
| Katherine Johnson | Mathematician |
| Rosalind Franklin | Chemist |
| Vera Rubin | Astronomer |
| Gladys West | Mathematician |
| Flossie Wong-Staal | Virologist and Molecular Biologist |
| Jennifer Doudna | Biochemist |
We’d like to put together the data to look like:
For the next few slides…
left_join()left_join()right_join()right_join()full_join()full_join()inner_join()inner_join()semi_join()semi_join()anti_join()anti_join()litF and GDP from Gapminder.join to merge two datasetsIf you ever need to understand which join is the right join for you, try to find an image that will lay out what the function is doing. I found this one that is quite good and is taken from the Statistics Globe blog.